package com.xuecheng.ucenter.service.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.ucenter.mapper.XcMenuMapper;
import com.xuecheng.ucenter.mapper.XcUserMapper;
import com.xuecheng.ucenter.model.dto.AuthParamsDto;
import com.xuecheng.ucenter.model.dto.XcUserExt;
import com.xuecheng.ucenter.model.po.XcMenu;
import com.xuecheng.ucenter.model.po.XcUser;
import com.xuecheng.ucenter.service.AuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;


import javax.xml.ws.Action;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户密码认证
 * @author 冯佳鑫 又菜又爱学
 * @version 1.0
 * @DATE now()
 */
@Slf4j
@Component
public class UserServiceImpl implements UserDetailsService {
    @Autowired
    XcUserMapper xcUserMapper;
    @Autowired
    ApplicationContext applicationContext;
    @Autowired
    XcMenuMapper xcMenuMapper;
    //请求过来的参数是AuthParamsDto
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        AuthParamsDto authParamsDto = null;
        //将json转为AuthParamsDto
        try {
             authParamsDto = JSON.parseObject(s, AuthParamsDto.class);
        } catch (Exception e) {
          throw new RuntimeException("请求参数不符合要求");
        }

        //取出认证类型
        String authType = authParamsDto.getAuthType();
        //根据认证类型从spring容器取出指定的bean
        String beanName = authType + "_authservice";
        AuthService authService = applicationContext.getBean(beanName, AuthService.class);
        //调用统一的execute方法完成认证
        XcUserExt xcUser = authService.execute(authParamsDto);
         //封装用户信息xcUser到userDetails对象==========
        UserDetails userPrincipal = this.getUserPrincipal(xcUser);


        return userPrincipal;
    }

    /**
     * @description 查询用户信息
     * @param xcUser  用户id，主键
     * @return com.xuecheng.ucenter.model.po.XcUser 用户信息
     * @author Mr.M
     * @date 2022/9/29 12:19
     */
    public UserDetails getUserPrincipal(XcUserExt xcUser){
        //创建userDetails对象=======
        //查询该用户的权限,只要code即可，并且装入authorities数组中
        String[] authorities = {};
        String id = xcUser.getId();
        List<XcMenu> xcMenus = xcMenuMapper.selectPermissionByUserId(id);
        ArrayList<String> objects = new ArrayList<>();
        for (XcMenu o :xcMenus) {
            String code = o.getCode();
            objects.add(code);
        }
        authorities = objects.toArray(new String[0]);

        //扩展用户信息，将user转为json格式,密码是敏感信息，要置空
        String password = xcUser.getPassword();
        xcUser.setPassword(null);
        String userString = JSON.toJSONString(xcUser);

        UserDetails build = User.withUsername(userString).password(password).authorities(authorities).build();
        return build;
    }
}
